package cn.kinggm520.controller;

import cn.kinggm520.domain.Goods;
import cn.kinggm520.entity.Result;
import cn.kinggm520.service.GoodsService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 作者: kinggm Email:731586355@qq.com
 * 时间:  2020-05-22 11:31
 */
@RestController
@RequestMapping("/goods")
@RefreshScope // 开启刷新功能
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @PostMapping("/add")
    @HystrixCommand(fallbackMethod = "addGoods_fallback")
    public Result addGoods(@RequestBody Goods goods) {
        goodsService.addGoods(goods);
        return new Result(true, "添加成功", goods);

    }

    /**
     * 定义降级方法：
     * 1. 方法的返回值需要和原方法一样
     * 2. 方法的参数需要和原方法一样
     * 3. 就是只有方法名不同
     */
    public Result addGoods_fallback(@RequestBody Goods goods) {
        return new Result(false, "添加失败,Goods服务端降级", goods);
    }


}
